package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ProjectSupplierDTO;
import com.financing.platform.dto.ProjectSupplierFlowDTO;
import com.financing.platform.service.IProjectSupplierService;
import com.financing.platform.util.DTOdateSet;

/**
 * 
 * @ClassName: ProjectCompanyAction
 * @Description: 产品供应商
 * @author lisu
 *
 */
@Controller("projectSupplierActionAPI")
@RequestMapping("/api/projectSupplier")
@Scope("prototype")
public class ProjectSupplierAction extends BaseAction {

	@Autowired
	private IProjectSupplierService projectSupplierServiceImpl;

	@RequestMapping("/addProjectSupplier.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request,
			HttpServletResponse response, ProjectSupplierDTO projectSupplierDTO) {
		try {
			log.info("In ProjectSupplierAction add begin!");
			
			ProjectSupplierDTO dto = projectSupplierServiceImpl.getProjectSupplierById(projectSupplierDTO);
			if(dto != null){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}

			// 初始化数据赋值
			DTOdateSet.setCreateInfo(projectSupplierDTO, request.getSession());
			DTOdateSet.setUpdateInfo(projectSupplierDTO, request.getSession());

			projectSupplierServiceImpl.addProjectSupplier(projectSupplierDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		} catch (Exception e) {
			log.error("ProjectCompanyAction add error !", e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		log.info("In ProjectCompanyAction add end!");
		return resultMap;
	}
	/**
	* 
	* @Title: update 
	* @Description: 修改产品供应商
	* @param   request
	* @param   response
	* @param   projectCompanyDTO
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/updateProjectSupplier.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request,
			HttpServletResponse response,ProjectSupplierDTO projectSupplierDTO) {
		
		log.info("In ProjectCompanyAction update begin!");
		
		try{
			if(projectSupplierDTO.getId() == null){
				resultMap.setStatus(STATUS.FAILE);
				log.info("In ProjectSupplierAction deleteCompany projectSupplierDTO id is null!");
				return resultMap;
			}
			ProjectSupplierDTO queryBean =  new ProjectSupplierDTO();
			queryBean.setAccessCompany(projectSupplierDTO.getAccessCompany());
			//queryBean.setQueryId(projectSupplierDTO.getId());
			ProjectSupplierDTO dto = projectSupplierServiceImpl.getProjectSupplierById(queryBean);
			if(dto != null &&  projectSupplierDTO.getId().intValue() != dto.getId().intValue() ){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			DTOdateSet.setUpdateInfo(projectSupplierDTO, request.getSession());
			projectSupplierServiceImpl.updateProjectSupplier(projectSupplierDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("ProjectCompanyAction updateprojectSupplier error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In ProjectCompanyAction updateprojectSupplier end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title: 查询公司供应商
	* @param   request
	* @param   response
	* @param   projectCompanyDTO
	* @return ResultMap   
	* @throws
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/listProjectSupplier.shtml")
	@ResponseBody
	public ResultMap query(HttpServletRequest request,
			HttpServletResponse response,ProjectSupplierDTO projectSupplierDTO,Pager pager) {
		
		log.info("In ProjectSupplierAction querylist begin!");
		
		try{
			
			projectSupplierDTO.setBegin(pager.getStartIndex());
			projectSupplierDTO.setPageSize(pager.getPageSize());
			
			
			int count = projectSupplierServiceImpl.queryProjectSupplierCount(projectSupplierDTO);
			
			pager.setTotalCount(count);
			
			List<ProjectSupplierDTO> list;
			if(count>0){
				list = projectSupplierServiceImpl.getProjectSupplierList(projectSupplierDTO);
			}else{
				list = Collections.EMPTY_LIST;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager",pager);
		}catch(Exception e){
			log.error("ProjectSupplierAction querylist error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In ProjectSupplierAction querylist end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title: queryCompany 
	* @Description: 查询公司供应商
	* @param   request
	* @param   response
	* @param   advertId
	* @return ResultMap   
	* @throws
	 */
	@RequestMapping("/queryProjectSupplierView.shtml")
	@ResponseBody
	public ResultMap queryProjectSupplierView(HttpServletRequest request,
			HttpServletResponse response,Integer id) {
		
		log.info("In ProjectSupplierAction queryProjectSupplierView Begin!");
		
		try{
			ProjectSupplierDTO projectSupplierDTO = new ProjectSupplierDTO();
			projectSupplierDTO.setId(id);
			ProjectSupplierDTO dto = projectSupplierServiceImpl.getProjectSupplierById(projectSupplierDTO);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("dto", dto);
		}catch(Exception e){
			log.error("ProjectSupplierAction queryProjectSupplierView error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In ProjectSupplierAction queryProjectSupplierView end!");	
		
		return resultMap;
	}
	/**
	* 
	* @Title: 查询公司供应商-历史
	* @param   request
	* @param   response
	* @param   projectCompanyDTO
	* @return ResultMap   
	* @throws
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/listProjectSupplierFlow.shtml")
	@ResponseBody
	public ResultMap listProjectSupplierFlow(HttpServletRequest request,
			HttpServletResponse response,ProjectSupplierFlowDTO projectSupplierFlowDTO,Pager pager) {
		
		log.info("In listProjectSupplierFlow  begin!");
		
		try{
			
			projectSupplierFlowDTO.setBegin(pager.getStartIndex());
			projectSupplierFlowDTO.setPageSize(pager.getPageSize());
			
			
			int count = projectSupplierServiceImpl.queryProjectSupplierFlowCount(projectSupplierFlowDTO);
			
			pager.setTotalCount(count);
			
			List<ProjectSupplierFlowDTO> list;
			if(count>0){
				list = projectSupplierServiceImpl.getProjectSupplierFlowList(projectSupplierFlowDTO);
			}else{
				list = Collections.EMPTY_LIST;
			}
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager",pager);
		}catch(Exception e){
			log.error("listProjectSupplierFlow  error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
		log.info("In listProjectSupplierFlow  end!");	
		
		return resultMap;
	}
}
